﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WikiCsharpCommon
{
    public partial class BugSelection : Form, Common.Forms.IBugSelection
    {
        private MyBugtracker m_mbt = null;
        private WikiCsharpBugtracker m_wcbt = new WikiCsharpBugtracker();

        public BugSelection(String parameters)
        {
            InitializeComponent();
            m_mbt = new MyBugtracker(new Settings.MySettingsManager(parameters));
        }

        public String BugId
        {
            get { return dgvList.SelectedRows[0].Cells["IssueID"].Value.ToString(); }
        }

        public String Message
        {
            get { return "-----\r\nBugID : " + BugId + " (" + (radioFixed.Checked ? radioFixed.Text : radioWIP.Text) + ")\r\n\r\n" + dgvList.SelectedRows[0].Cells["IssueSummary"].Value.ToString(); }
        }

        private void BugSelection_Load(object sender, EventArgs e)
        {
            cboStatus.Items.Add("");
            cboStatus.Items.AddRange(m_wcbt.GetStatusList(m_mbt.Settings.Folder));

            cboAssignee.Items.Add("");
            cboAssignee.Items.AddRange(m_wcbt.GetUserFullNameList(m_mbt.Settings.Folder));

            cboReporter.Items.Add("");
            cboReporter.Items.AddRange(m_wcbt.GetUserFullNameList(m_mbt.Settings.Folder));

            cboAssignee.SelectedItem = m_wcbt.GetUserFullName(m_mbt.Settings.Folder, Environment.UserName);
            cboReporter.SelectedItem = m_wcbt.GetUserFullName(m_mbt.Settings.Folder, Environment.UserName);

            int default_status_index = m_wcbt.GetWIPStatusIndex(m_mbt.Settings.Folder) + 1;
            if (cboStatus.Items.Count > default_status_index)
                cboStatus.SelectedIndex = default_status_index;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {

        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows.Count > 0)
                btnOk.Enabled = true;
        }

        public bool AddCheckIn(string source_base_path, string note, string revision, string[] filelist)
        {
            if (!string.IsNullOrEmpty(source_base_path))
            {
                note += string.Format("{0} - ", source_base_path);
            }

            note += string.Format("Revision : {0}{1}", revision, Environment.NewLine);

            if (filelist.Length > 0)
            {
                note += " -----Files list----- ";

                foreach (string s in filelist)
                    note += Environment.NewLine + s;
            }

            m_wcbt.AddCheckin(m_mbt.Settings.Folder, BugId, note, revision);

            if (radioFixed.Checked)
            {
                String[] statuses = m_wcbt.GetStatusList(m_mbt.Settings.Folder);
                String actual_status = dgvList.SelectedRows[0].Cells["IssueStatus"].Value.ToString();

                int default_status_index = m_wcbt.GetDoneStatusIndex(m_mbt.Settings.Folder);
                m_wcbt.ChangeBugState(m_mbt.Settings.Folder, this.BugId, statuses[default_status_index]);

                m_wcbt.SetBugCorrected(m_mbt.Settings.Folder, this.BugId);
            }

            return true;
        }

        private void cboStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            FeedList();

            UpdateFilter();
        }

        private void FeedList()
        {
            dsData.dtIssueData.Clear();

            if (string.IsNullOrEmpty(cboStatus.Text))
            {
                for (int i = 1; i < cboStatus.Items.Count; ++i)
                {
                    dsData.dtIssueData.Merge(m_wcbt.GetBugList(m_mbt.Settings.Folder, cboStatus.Items[i].ToString()));
                }
            }
            else
            {
                dsData.dtIssueData.Merge(m_wcbt.GetBugList(m_mbt.Settings.Folder, cboStatus.Text));
            }
        }

        private void filter_Changed(object sender, EventArgs e)
        {
            UpdateFilter();
        }

        private void UpdateFilter()
        {
            string filter = "IssueID LIKE '*" + txtIDFilter.Text + "*'";

            if(!String.IsNullOrEmpty(txtSummary.Text))
                filter += " AND IssueSummary LIKE '*" + txtSummary.Text + "*'";

            if (!string.IsNullOrEmpty(cboAssignee.Text) && !string.IsNullOrEmpty(cboReporter.Text))
            {
                if (chkOR.Checked)
                {
                    filter += " AND (IssueAssignee = '" + cboAssignee.Text + "' OR IssueReporter = '" + cboReporter.Text + "')";
                }
                else
                {
                    filter += " AND IssueAssignee = '" + cboAssignee.Text + "' AND IssueReporter = '" + cboReporter.Text + "'";
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(cboAssignee.Text))
                {
                    filter += " AND IssueAssignee = '" + cboAssignee.Text + "'";
                }
                else if (!string.IsNullOrEmpty(cboReporter.Text))
                {
                    filter += " AND IssueReporter = '" + cboReporter.Text + "'";
                }
            }
            
            tIssuesBindingSource.RowFilter = filter;
        }
    }
}
